
from skimage import io,transform
import glob
import os
import tensorflow as tf
import numpy as np
import time

# import os    #os 处理文件和目录的模块
# import glob  #glob 文件通配符模块
# 此程序作用于进行简单的预测，取5个图片来进行预测（不知道预测多个要怎么改）
path1 = "flower_photos/daisy/5547758_eea9edfd54_n.jpg"
path2 = "flower_photos/dandelion/7355522_b66e5d3078_m.jpg"
path3 = "flower_photos/roses/394990940_7af082cf8d_n.jpg"
path4 = "flower_photos/sunflowers/6953297_8576bf4ea3.jpg"
path5 = "flower_photos/tulips/10791227_7168491604.jpg"

# 类别代表字典
flower_dict = {0: 'dasiy', 1: 'dandelion', 2: 'roses', 3: 'sunflowers', 4: 'tulips'}

w = 100
h = 100
c = 3


# 将读取的每个图片转化为数据
def read_one_image(path):  # io.imread(im)读取单张RGB图片
    img = io.imread(path)  # skimage.io.imread(fname,as_grey=True)读取单张灰度图片
    img = transform.resize(img, (w, h))  # skimage.transform.resize(image, output_shape)改变图片的尺寸
    return np.asarray(img)


# 找到已有的模型，进行读取

with tf.Session() as sess:
    data = []
    data1 = read_one_image(path1)
    data2 = read_one_image(path2)
    data3 = read_one_image(path3)
    data4 = read_one_image(path4)
    data5 = read_one_image(path5)
    data.append(data1)
    data.append(data2)
    data.append(data3)
    data.append(data4)
    data.append(data5)
    saver = tf.train.import_meta_graph(r'E:\python\tcl\tf_text\tf_card\medl\model.meta')
    saver.restore(sess, tf.train.latest_checkpoint(r'E:\python\tcl\tf_text\tf_card\medl'))
    # sess：表示当前会话，之前保存的结果将被加载入这个  #设置每次预测的个数
    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("x:0")
    feed_dict = {x: data}

    logits = graph.get_tensor_by_name("logits_eval:0")

    classification_result = sess.run(logits, feed_dict)
    # 打印出预测矩阵
    print(classification_result)
    # 打印出预测矩阵每一行最大值的索引
    print(tf.argmax(classification_result, 1).eval())
    # 根据索引通过字典对应花的分
    output = []
    output = tf.argmax(classification_result, 1).eval()
    print(output)
    print(output.shape)
    for i in range(len(output)):
        print("flower", i + 1, "prediction:" + flower_dict[output[i]])
